export default {
  data () {
    return {
      dustEchart: null,
      TSPDataMap: new Map(),
      PMTenDataMap: new Map(),
      PMTwoPointFiveDataMap: new Map(),
      dustOption: {
        title: {
          text: '近7天扬尘变化情况(单位：μg/m3)',
          textStyle: {
            color: '#363c3c',
            fontSize: 14,
            fontWeight: 'normal',
            fontFamily: 'Microsoft YaHei'
          }
        },
        tooltip: {
          trigger: 'axis',
          confine: true,
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#6a7985'
            }
          }
        },
        legend: {
          top: 20,
          right: 0,
          itemWidth: 25,
          itemHeight: 2,
          padding: [7, 0, 0, 0],
          textStyle: {
            color: '#898989'
          },
          data: [{name: 'TSP', icon: 'rect'}, {name: 'PM10', icon: 'rect'}, {name: 'PM2.5', icon: 'rect'}]
        },
        grid: {
          left: 20,
          right: 20,
          bottom: 0,
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: false,
            data: [],
            axisLabel: {
              color: '#939393'
            },
            axisLine: {
              show: true,
              lineStyle: {
                color: '#e8e8e8',
                width: 2
              }
            },
            axisTick: {
              show: false
            },
            splitLine: {
              show: false,
              lineStyle: {
                color: '#a8c5ed'
              }
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '',
            nameTextStyle: {
              color: '#a8c5ed'
            },
            axisLabel: {
              show: false,
              color: '#a8c5ed'
            },
            axisTick: {
              show: false
            },
            axisLine: {
              show: false,
              lineStyle: {
                color: '#214a60',
                width: 2
              }
            },
            splitLine: {
              lineStyle: {
                type: 'dashed',
                color: '#e8e8e8'
              }
            },
            position: 'right'
          }
        ],
        series: []
      }
    }
  },
  methods: {
    initDust () {
      if (this.dustEchart) {
        this.dustOption.series.forEach(item => {
          item.data = []
        })
      }
      this.initDustTime()
      this.initDustData()
      this.tsp()
      this.pmTen()
      this.pmTwoPointFive()
      this.dustEchart = this.$Echarts.init(this.$refs.dust)
      this.dustEchart.setOption(this.dustOption)
    },
    initDustTime () {
      this.dustOption.xAxis[0].data = []
      for (let i = 0; i < 7; i++) {
        let time = this.$moment().subtract(6, 'days').add(i, 'days').format('YYYY-MM-DD')
        let DD = this.$moment().subtract(i, 'days').format('DD')
        this.TSPDataMap.set(time, 0)
        this.PMTenDataMap.set(time, 0)
        this.PMTwoPointFiveDataMap.set(time, 0)
        if (i === 0) {
          this.dustOption.xAxis[0].data.unshift(`今日`)
        } else if (i === 1) {
          this.dustOption.xAxis[0].data.unshift(`昨天`)
        } else {
          this.dustOption.xAxis[0].data.unshift(`${DD}日`)
        }
      }
    },
    initDustData () {
      this.dustWorksumbydayVOS.forEach((item) => {
        let time = this.$moment(item.updateTime * 1000).format('YYYY-MM-DD')
        this.TSPDataMap.set(time, item.tspAvg)
        this.PMTenDataMap.set(time, item.pmTenAvg)
        this.PMTwoPointFiveDataMap.set(time, item.pmTwoPointFiveAvg)
      })
    },
    tsp () {
      let TSPData = [...this.TSPDataMap.values()]
      let TSPSeries = {
        name: 'TSP',
        type: 'line',
        areaStyle: {
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [{
              offset: 0, color: 'rgba(215, 244, 248, 0.9)' // 0% 处的颜色
            }, {
              offset: 1, color: 'rgba(215, 244, 248, 0.1)' // 100% 处的颜色
            }],
            globalCoord: false // 缺省为 false
          }
        },
        itemStyle: {
          color: '#33cadb',
          opacity: 0
        },
        data: TSPData
      }
      this.dustOption.series.push(TSPSeries)
    },
    pmTen () {
      let PMTenData = [...this.PMTenDataMap.values()]
      let PMTenSeries = {
        name: 'PM10',
        type: 'line',
        areaStyle: {
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [{
              offset: 0, color: 'rgba(183, 241, 229, 0.7)' // 0% 处的颜色
            }, {
              offset: 1, color: 'rgba(183, 241, 229, 0.1)' // 100% 处的颜色
            }],
            globalCoord: false // 缺省为 false
          }
        },
        itemStyle: {
          color: '#4bdaa4',
          opacity: 0
        },
        data: PMTenData
      }
      this.dustOption.series.push(PMTenSeries)
    },
    pmTwoPointFive () {
      let PMTwoPointFiveData = [...this.PMTwoPointFiveDataMap.values()]
      let PMTwoPointFiveSeries = {
        name: 'PM2.5',
        type: 'line',
        areaStyle: {
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [{
              offset: 0, color: 'rgba(200, 239, 244, 0.7)' // 0% 处的颜色
            }, {
              offset: 1, color: 'rgba(200, 239, 244, 0.1)' // 100% 处的颜色
            }],
            globalCoord: false // 缺省为 false
          }
        },
        itemStyle: {
          color: '#60bbf0',
          opacity: 0
        },
        data: PMTwoPointFiveData
      }
      this.dustOption.series.push(PMTwoPointFiveSeries)
    }
  }
}
